package com.swas.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.swas.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("userInfo");
        if (user == null){
            PrintWriter writer = response.getWriter();
            ObjectMapper om = new ObjectMapper();
            writer.write(om.writeValueAsString(ResultVO.noAuth("403 Forbidden")));
            return false;
        }
        return true;
    }
}
